import 'package:base_plugin/base_plugin.dart';
import 'package:finance_app/src/common/app_colors.dart';
import 'package:finance_app/src/common/app_spaces.dart';
import 'package:finance_app/src/utils/input_formatter_tools.dart';
import 'package:finance_app/src/widgets/common/tag_widget.dart';
import 'package:flutter/material.dart';

class ChartAccountOpenBalanceItem extends StatelessWidget {
  final String name;
  final String title;
  final String amount;
  final String hardAmount;
  final VoidCallback onTap;
  final String status;
  final ValueChanged<String> onUpdateValue;
  final String currencyCode;
  final bool enabled;
  final bool isChild;

  const ChartAccountOpenBalanceItem({
    Key key,
    this.name,
    this.onTap,
    this.title,
    this.amount,
    this.hardAmount,
    this.status,
    this.onUpdateValue,
    this.currencyCode,
    this.enabled,
    this.isChild = false,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return PressedOpacityButton(
      onPressed: onTap,
      color: Colors.white,
      padding: EdgeInsets.only(
        left: isChild ? getDp(55) : getDp(35),
        top: getDp(15),
        right: getDp(35),
        bottom: getDp(15),
      ),
      border: Border(
        bottom: BorderSide(color: aceGrayColor, width: getDp(0.5)),
      ),
      child: Row(
        crossAxisAlignment: CrossAxisAlignment.center,
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          Expanded(
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Text(
                  name,
                  style: TextStyle(
                    color: aceBlackColor,
                    fontSize: getSp(16),
                    fontWeight: FontWeight.w400,
                    height: 14 / 16,
                  ),
                ),
                AppSpaces.h10,
                Row(
                  children: [
                    Text(
                      title,
                      style: TextStyle(
                        color: aceDarkGrayColor.withOpacity(0.7),
                        fontSize: getSp(16),
                        fontWeight: FontWeight.w400,
                        height: 14 / 16,
                      ),
                    ),
                    if (status != null)
                      Padding(
                        padding: EdgeInsets.only(left: getDp(10)),
                        child: BlueTagWidget(
                          content: status,
                        ),
                      )
                  ],
                ),
              ],
            ),
          ),
          Container(
            constraints: BoxConstraints(
              maxWidth: getDp(120),
            ),
            child: PrefixInputField(
              prefix: currencyCode,
              initValue: amount,
              hardAmount: hardAmount,
              onUpdateValue: onUpdateValue,
              enabled: enabled ?? true,
            ),
          )
        ],
      ),
    );
  }
}

class PrefixTextEditController extends TextEditingController {
  final String prefix;

  PrefixTextEditController({this.prefix, String text}) : super(text: text);

  @override
  TextSpan buildTextSpan({BuildContext context, TextStyle style, bool withComposing}) {
    print(withComposing);
    return TextSpan(children: [
      if (prefix != null)
        TextSpan(
          text: '$prefix ',
          style: TextStyle(
            color: aceBlueColor,
            fontSize: getSp(16),
            fontWeight: FontWeight.w700,
            height: 14 / 16,
          ),
        ),
      super.buildTextSpan(context: context, style: style, withComposing: withComposing),
    ]);
  }
}

class PrefixInputField extends StatefulWidget {
  final String initValue;
  final String prefix;
  final String hardAmount;
  final ValueChanged<String> onUpdateValue;
  final bool enabled;
  final double fontSize;
  final TextAlign textAlign;

  const PrefixInputField(
      {Key key, this.initValue, this.hardAmount, this.prefix, this.onUpdateValue, this.enabled = true, this.fontSize = 16, this.textAlign = TextAlign.right})
      : super(key: key);

  @override
  _PrefixInputFieldState createState() => _PrefixInputFieldState();
}

class _PrefixInputFieldState extends State<PrefixInputField> {
  // PrefixTextEditController controller;

  @override
  void initState() {
    super.initState();
    // controller =
    //     PrefixTextEditController(prefix: widget.prefix, text: widget.initValue);
  }

  @override
  Widget build(BuildContext context) {
    if (widget.hardAmount != null)
      return Text(
        widget.hardAmount ?? '',
        style: TextStyle(fontSize: getSp(widget.fontSize), color: aceBlackColor, fontWeight: FontWeight.w700, height: 14 / widget.fontSize),
      );
    else
      return TextFormField(
        // controller: controller,
        initialValue: widget.initValue,
        inputFormatters: [
          doubleLimit2InputFormatter,
        ],
        onChanged: (value) {
          widget.onUpdateValue?.call(value);
        },
        scrollPadding: EdgeInsets.zero,
        obscureText: false,
        keyboardType: TextInputType.numberWithOptions(decimal: true),
        enabled: widget.enabled,
        style: TextStyle(
            fontSize: getSp(widget.fontSize), color: widget.enabled ? aceBlueColor : aceBlackColor, fontWeight: FontWeight.w700, height: 14 / widget.fontSize),
        textAlign: widget.textAlign,
        enableSuggestions: true,
        decoration: InputDecoration(
            border: InputBorder.none,
            focusedBorder: InputBorder.none,
            disabledBorder: InputBorder.none,
            isDense: true,
            alignLabelWithHint: true,
            isCollapsed: true,
            prefixStyle: TextStyle(
              color: widget.enabled ? aceBlueColor : aceBlackColor,
              fontSize: getSp(widget.fontSize),
              fontWeight: FontWeight.w700,
              height: 14 / widget.fontSize,
            ),
            prefixText: widget.prefix),
      );
  }

// @override
// void didUpdateWidget(covariant PrefixInputField oldWidget) {
//   super.didUpdateWidget(oldWidget);
// }
}
